<?php

class Url extends Controller {

	function Url() {
		parent::Controller();	

		//loads the form validation library class
		$this->load->library("form_validation");
		//loads the model class
		$this->load->model("miniaster_common");
		//loads the helper class
		$this->load->helper("common_helper");
	}
	
	function index() {
		$this->_display_default_page();
	} //end of function index()

	function encode() {

		$user_url = trim($this->input->post("tf_inurl"));
		$user_keyword = trim($this->input->post("tf_keyword"));
		$user_src_ip = $this->input->ip_address();
		$user_agent = $this->input->user_agent();

		//checks whether the form has been submited
		if(isMiniAsterValidUrl($user_url)) {

			//validation rules
			$rules = array(
				array('field'=>'tf_inurl', 'label'=>'lbl_url', 'rules'=>'required'),
				array('field'=>'tf_keyword', 'label'=>'lbl_keyword', 'rules'=>'callback__isValidKeyword')
			);

			//setting the validation rules inside the validation function
			$this->form_validation->set_rules($rules);
			$this->form_validation->set_error_delimiters('<em class="formerr">', '</em>');

			//check whether the form is properly sent
			if($this->form_validation->run() == FALSE) {
				//if validation fails then load the login form again
				$this->_display_default_page();
			} else {
				$keyword = $this->miniaster_common->checkDuplicateUrl($user_url);
				
				if ($keyword == NULL) {
					$this->miniaster_common->updateKeyword($user_url, $user_keyword);
					$keyword = $user_keyword;
				}
				
				if ($keyword == -1) {
					$keyword = $this->miniaster_common->addUrl($user_url, $user_keyword, $user_src_ip, $user_agent);
				}
				
				$shorten_url = site_url("$keyword");

				$this->session->set_flashdata('message', "Your Shorten URL is: $shorten_url"); 
				redirect('/', 'refresh');
			}	

		} else {
			//form has not been submited or thr URL is not a valid URL as per our checks.
			$this->session->set_flashdata('message', "Invalid URL. We do not allow our own URLs, URLs need to be <a href='http://www.faqs.org/rfcs/rfc2396' target='_blank'>RFC2396</a> compliant with a valid flag scheme like 'http://'");
			redirect('/', 'refresh');
		}
	} //end function shortenurl()

	function decode($keyword) {
		$user_src_ip = $this->input->ip_address();
		$user_agent = $this->input->user_agent();
		//get the url from database
		$url = $this->miniaster_common->getUrl($keyword, $user_src_ip, $user_agent);

		if ($url == -1) {
			$this->session->set_flashdata('message', "Invalid keyword, no short URL \"$keyword\" found.");
			redirect('/', 'refresh');
		} else {
			//redirect to actual url
			header("Location: $url", TRUE, 301);
			exit(0);
		}
	} //end function keyword_to_url()

	function _display_default_page() {
		$page_data = array(
			"num_redirects" => $this->miniaster_common->getTotalRedirects(), 
			"num_urls" => $this->miniaster_common->getUrlCount()
		);
		$this->load->view("miniaster_view", $page_data);
	} //end function _display_default_page()

	function _isValidKeyword($keyword) {
		if (empty($keyword)) {
			return TRUE;
		} else {
			$offensive_keyword = $this->miniaster_common->isBlocked($keyword);
			if($offensive_keyword) {
				$this->form_validation->set_message("_isValidKeyword", "This is an blocked keyword. Try again or leave the keyword field blank to auto-generate.");
				return FALSE;
			} else {
				$keyword_available = $this->miniaster_common->isKeywordAvailable($keyword);
			
				if($keyword_available) {
					return TRUE;
				} else {
					$this->form_validation->set_message("_isValidKeyword", "This keyword is not available. Try again or leave the keyword field blank to auto-generate.");
					return FALSE;
				}
			}
		}
		$this->form_validation->set_message("_isValidKeyword", "OOps!!! Looks like somethings is wrong, you should have never seen this. Please report to webmaster and retry.");
		return FALSE;
		
	} //end function _isValidKeyword()
} //end class Url
